package org.wxd.bi.api.struct;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.wxd.aop.Aop;
import org.wxd.batis.EntityField;
import org.wxd.batis.enums.ColumnType;
import org.wxd.batis.sql.SqlDataWrapper;
import org.wxd.batis.sql.SqlEntityTable;
import org.wxd.batis.sql.mysql.MysqlDataWrapper;
import org.wxd.batis.sql.mysql.MysqlFactory;
import org.wxd.batis.struct.DbColumn;
import org.wxd.batis.struct.DbTable;
import org.wxd.field.extend.FieldAnn;
import org.wxd.lang.save.ObjectBase;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 远程push数据映射
 *
 * @author: Troy.Chen(無心道, 15388152619)
 * @version: 2021-09-28 22:24
 **/
@Slf4j
@Getter
@Setter
@Accessors(chain = true)
@DbTable(comment = "数据映射关系")
public class PushMapping extends ObjectBase implements Serializable, Comparable<PushMapping> {

    private static final long serialVersionUID = 1L;

    @DbColumn(key = true, comment = "映射表名")
    private String tableName;
    @DbColumn(comment = "表描述")
    private String tableComment;
    @DbColumn(comment = "显示顺序")
    private int showIndex = 999;
    @DbColumn(comment = "显示分组")
    private String showGroup = "日志查询";
    /**
     * <p>字段名字    英文的名字
     * <p>字段类型     string, int ,long
     * <p>字段备注    注释，备注，
     * <p>是否加索引 true or false 默认用false
     */
    @DbColumn(columnType = ColumnType.Json, comment = "映射字段")
    private List<PushColumn> columnList = new LinkedList<>();

    @FieldAnn(alligator = true)
    @DbColumn(alligator = true)
    @JSONField(serialize = false, deserialize = false)
    private transient SqlEntityTable sqlEntityTable = null;

    public SqlEntityTable mapping() throws Exception {
        if (sqlEntityTable == null) {

            sqlEntityTable = new SqlEntityTable(MysqlDataWrapper.Default);
            sqlEntityTable.setTableName(this.tableName);
            sqlEntityTable.setTableComment(this.tableComment);
            final SqlDataWrapper<SqlEntityTable> dataWrapper = Aop.getBean(MysqlFactory.class)
                    .dataHelper()
                    .getDataWrapper();
            for (PushColumn tEntry4 : columnList) {
                EntityField dataColumnMapping = new EntityField()
                        .setColumnName(tEntry4.getName())/*名字 */
                        .setColumnComment(tEntry4.getComment())/*备注*/
                        .setColumnIndex(tEntry4.isDbIndex())//是否是索引
                        ;
                if (PushColumn.ID.getName().equals(tEntry4.getName()) || tEntry4.isPk()) {
                    dataColumnMapping.setColumnKey(true);
                    sqlEntityTable.setDataColumnKey(dataColumnMapping);
                }
                /*构建数据类型*/
                dataWrapper.buildColumnType(dataColumnMapping, tEntry4.getTypeStr().toLowerCase());
                if (dataColumnMapping.getColumnType() == ColumnType.None) {
                    throw new RuntimeException("预设表 无法预知的数据类型：" + tEntry4.getTypeStr());
                }
                sqlEntityTable.getColumnMap().put(dataColumnMapping.getColumnName(), dataColumnMapping);
            }

        }
        return sqlEntityTable;
    }

    public void sortColumn() {
        columnList.sort(null);
    }

    public Map<String, String[]> htmlColumnInfo() throws Exception {
        Map<String, String[]> cul = new LinkedHashMap();
        for (PushColumn pushColumn : columnList) {
            cul.put(pushColumn.getName(), new String[]{pushColumn.getComment(), pushColumn.getHtmlStyle().toString()});
        }
        return cul;
    }

    public Map<String, String> excelColumnInfo() {
        Map<String, String> cul = new LinkedHashMap();
        for (PushColumn pushColumn : columnList) {
            cul.put(pushColumn.getName(), pushColumn.getComment());
        }
        return cul;
    }

    @Override
    public int compareTo(PushMapping o) {
        return Integer.compare(this.showIndex, o.showIndex);
    }
}
